using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerControl : MonoBehaviour
{
    public static playerControl Instance;
    public Animator ani;
    private Rigidbody2D rbody;
    // 是否踩在地面
    private bool isGround;
    // 子弹预设体
    public GameObject BulletPro;

    void Awake()
    {
        Instance = this;
        ani = GetComponent<Animator>();
        rbody = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        // 判断人物是否死亡
        var isdie = ani.GetBool("die");
        if(!isdie){
            // 人物移动
            float horizontal = Input.GetAxis("Horizontal");
            rbody.velocity = new Vector2(horizontal,rbody.velocity.y);
            // 人物方向
            var scale = transform.localScale;
            if(horizontal>0){
                scale.x = -1;
            }
            if(horizontal<0){
                scale.x = 1;
            }
            transform.localScale = scale;
            // 站立，跑步动画切换
            ani.SetBool("isRun",rbody.velocity.x==0?false:true);
            // 人物跳跃
            if(Input.GetKeyDown(KeyCode.I) && isGround){
                rbody.velocity = new Vector2(rbody.velocity.x,2);
            }
            // 射击
            if(Input.GetKeyDown(KeyCode.U)){
                Instantiate(BulletPro,transform.GetChild(0).transform.position,Quaternion.identity);
            }
        }else{
            BossControl.Instance.distance = 0;
            if (EnemyPoint.Instance != null) EnemyPoint.Instance.isTimerActive = false;
            if (EnemyPoint1.Instance != null) EnemyPoint1.Instance.isTimerActive = false;
            if (EnemyControl.Instance != null) EnemyControl.Instance.isFly = false;
            if (EnemyControl1.Instance != null) EnemyControl1.Instance.isFly = false;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.collider.tag == "Ground"){
            isGround = true;
        }
        if(collision.collider.tag == "Enemy"){
            ani.SetBool("die",true);
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if(collision.collider.tag == "Ground"){
            isGround = false;
        }
    }
}
